﻿using System.Windows;
using System.Windows.Input;
using FastColoredTextBoxNS;


namespace MPE
{
    public partial class Settings : Window
    {
        readonly FastColoredTextBox field;
        readonly System.Windows.Controls.Grid grid;
        readonly Properties.Settings param = Properties.Settings.Default;
        public Settings(ref System.Windows.Controls.Grid grid, FastColoredTextBox field)
        {
            InitializeComponent();
            this.grid = grid;
            this.field = field;
            field.SizeChanged += Field_SizeChanged;
            popupMenuTimer.Text = param.popupMenuTimer.ToString();
            if (param.taskFoler.Length > 0) taskForldLB.ToolTip = taskForldLB.Content = param.taskFoler;
            else
            {
                taskForldLB.ToolTip = taskForldLB.Content = "Папка не указана!";
                taskForldLB.Foreground = System.Windows.Media.Brushes.Red;
            }

            mapSize.Content = (int)codeMapSize.Value + "px";
            errorTimer.Text = param.errorTimer.ToString();
            if (!param.isCodeMap)
            {
                showMSG.Visibility = Visibility.Visible;
                mapSize.Visibility = codeMapSize.Visibility = Visibility.Hidden;
            }

            isErrorTimer.IsChecked = param.isErrorTimer;

            codeMapSize.ValueChanged += CodeMapSize_ValueChanged;
        }
        void Field_SizeChanged(object sender, System.EventArgs e)
        {
            if (param.isCodeMap)
            {
                showMSG.Visibility = Visibility.Hidden;
                mapSize.Visibility = codeMapSize.Visibility = Visibility.Visible;
            }
            else
            {
                showMSG.Visibility = Visibility.Visible;
                mapSize.Visibility = codeMapSize.Visibility = Visibility.Hidden;
            }
        }
        void UpdatesCB_Click(object sender, RoutedEventArgs e) { param.isCheckUpdates = !param.isCheckUpdates; updatesCB.IsChecked = !updatesCB.IsChecked; param.Save(); }
        void FontChange_Click(object sender, RoutedEventArgs e)
        {
            using (System.Windows.Forms.FontDialog fontDialog = new System.Windows.Forms.FontDialog())
            {
                if (fontDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                { param.fieldFont = field.Font = fontDialog.Font; param.Save(); }
            }
        }
        void CodeMapSize_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (param.isCodeMap)
            {
                codeMapSize.ToolTip = "Ширина: " + codeMapSize.Value;
                param.codeMapSize = (int)codeMapSize.Value;
                mapSize.Content = (int)codeMapSize.Value + "px";
                grid.ColumnDefinitions[2].Width = new GridLength(codeMapSize.Value);
                param.Save();
            }
        }
        void SetTaskFolder_Click(object sender, RoutedEventArgs e)
        {
            using (System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog())
                if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    taskForldLB.ToolTip = taskForldLB.Content = fbd.SelectedPath;
                    taskForldLB.Foreground = System.Windows.Media.Brushes.White;
                    param.taskFoler = fbd.SelectedPath; param.Save();
                }
        }
        void MakeBackUpCB_Click(object sender, RoutedEventArgs e) { param.isMakeBackUp = makeBackUpCB.IsChecked == true; param.Save(); }
        void OpenLastFileCB_Click(object sender, RoutedEventArgs e) { param.isOpenLastFile = openLastFileCB.IsChecked == true; param.Save(); }
        void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) => DragMove();
        void SaveWindowSizeCB_Click(object sender, RoutedEventArgs e) { param.isSaveWindowSize = saveWindowSizeCB.IsChecked == true; param.Save(); }
        void ForumTopic_Click(object sender, RoutedEventArgs e) => System.Diagnostics.Process.Start("https://djoser-blog.ru/forum/index.php?/topic/7174-redaktor-shablonov-minipostereditor/");
        void GitHubSource_Click(object sender, RoutedEventArgs e) => System.Diagnostics.Process.Start("https://github.com/Medoo48/miniPosterEditor");
        void GitHubUpdaterSource_Click(object sender, RoutedEventArgs e) => System.Diagnostics.Process.Start("https://github.com/Medoo48/MPE_updater/");
        void ErrorTimer_LostFocus(object sender, RoutedEventArgs e)
        {
            if (!int.TryParse((sender as System.Windows.Controls.TextBox).Text, out int timer) || timer <= 0) return;
            DataHelper.setErrorTimer(param.errorTimer = timer);
        }
        void CheckUpdates_Click(object sender, RoutedEventArgs e) => Updater.FindNewVersion(true);
        void ExecTimer_Click(object sender, RoutedEventArgs e)
        {
            if (int.TryParse(popupMenuTimer.Text, out int ms) && ms > 0)
            {
                AutoCompleete.setTimer(ms);
                param.popupMenuTimer = ms;
                param.Save();
            }
            else MessageBox.Show("Число должно быть целым, положительным и больше нуля");
        }
        void isErrorTimer_Click(object sender, RoutedEventArgs e) { param.isErrorTimer = isErrorTimer.IsChecked == true; param.Save(); }
    }
}